package com.kaoqin.util;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Workbook;

import net.sf.jxls.exception.ParsePropertyException;
import net.sf.jxls.transformer.XLSTransformer;

public class JxlsExcelUtil {
	/**
	 * 后台日志
	 */
	protected static Log logger = LogFactory.getLog(JxlsExcelUtil.class);
	private static URL classUrl = JxlsExcelUtil.class.getResource("/");
	/**
	 * 配置
	 */
	protected static Properties p = new Properties();
	/**
	 * 映射关系对照配置文件路径
	 */
	private static final String MAPPINGFILEPATH = "/jxls_excel.properties";
	/**
	 * 模板KEY和模板所在路径
	 */
	private static Map<String, String> jxlsMap = new HashMap<String, String>(0);
	static {
		InputStream in = null;
		try {
			in = JxlsExcelUtil.class.getClassLoader().getResourceAsStream(MAPPINGFILEPATH);
			if (in != null)
				p.load(in);
		} catch (IOException e) {
			logger.error("load " + MAPPINGFILEPATH + " into Constants error!");
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					logger.error("close " + MAPPINGFILEPATH + " error!");
				}
			}
			if (p.keySet() != null && p.keySet().size() > 0) {
				for (Object object : p.keySet()) {
					String key = object == null ? "" : object.toString().trim();
					String value = "";
					if (!"".equals(key))
						value = p.getProperty(key);
					if (value != null && !"".equals(value)) {
						jxlsMap.put(key, value);
					}
				}
			}
		}
	}

	public static Map<String, String> getUtilMap() {
		return jxlsMap;
	}
	/**
	 * 该方法是数据格式一样，但是sheet展示的数据不一样
	 * @param arg1 数据集合
	 * @param arg2 sheet
	 * @param arg3 action名称
	 * @param arg4 sheet从几开始
	 * @param arg5 sheet模板里的变量名称
	 * @return workbook
	 * @throws ParsePropertyException
	 * @throws InvalidFormatException
	 * @throws IOException
	 */
	public static Workbook transformMultipleSheetsList(List arg1, List arg2, String arg3, int arg4, String arg5)
			throws RuntimeException {
		Workbook workbook = null;
		XLSTransformer transformer = new XLSTransformer();
		InputStream in;
		try {
			in = getTemplateInputStream(arg3);
		} catch (IOException e1) {
			throw new RuntimeException("创建模板异常！");
		}
		try {
			workbook = transformer.transformMultipleSheetsList(in, arg1, arg2, arg5, null, arg4);
		} catch (ParsePropertyException e) {
			throw new RuntimeException("属性使用异常!");
		} catch (InvalidFormatException e) {
			throw new RuntimeException("格式化数据异常!");
		} finally {
			try {
				in.close();
			} catch (IOException e) {
				throw new RuntimeException("关闭文件流异常!");
			}
		}
		return workbook;
	}
	/**
	 * 该集合支持多个对象生成execl，根据模板生成每个sheet
	 * @param beanParams 数据集合
	 * @param sheetListName sheet 名称
	 * @param arg3 action名称
	 * @return workbook
	 * @throws RuntimeException
	 */
	public static Workbook transformMultipleSheetsListByMap(Map<String, List> beanParams, List<String> sheetListName,
			InputStream in) throws RuntimeException {
		Workbook workbook = null;
		XLSTransformer transformer = new XLSTransformer();
		try {
			workbook = transformer.transformXLS(in, beanParams);
		} catch (ParsePropertyException e) {
			throw new RuntimeException("属性使用异常!");
		} catch (InvalidFormatException e) {
			throw new RuntimeException("格式化数据异常!");
		} finally {
			try {
				in.close();
			} catch (IOException e) {
				throw new RuntimeException("关闭文件流异常!");
			}
		}
		for (int i = 0; i < sheetListName.size(); i++) {
			workbook.setSheetName(i, sheetListName.get(i));
		}
		return workbook;
	}
	/**
	 * 获取文件模板流
	 * @param urlKey
	 * @return
	 * @throws IOException
	 */
	public static InputStream getTemplateInputStream(String urlKey) throws IOException {
		String url1 = JxlsExcelUtil.class.getResource("/").getPath();
		//WEB-INF/classes/
		String moban = url1.replace("/WEB-INF/classes/", "/jxls_module");
		moban = moban.replace("/", File.separator);
		moban = moban.concat(File.separator).concat(getUtilMap().get(urlKey));
		InputStream in = null;
		InputStream instream = new FileInputStream(moban);
		in = new BufferedInputStream(instream);
		//	instream.close();
		return in;
	}
}
